package com.qunar.train.mock.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author: xudong.tai
 * @since: 2018/7/6 Description:
 **/
public class AegisLog {

    public static void error(Exception e) {
        try {
            String clazzName = Thread.currentThread().getStackTrace()[2].getClassName();
            String methodName = Thread.currentThread().getStackTrace()[2].getMethodName();
            int linenumber = Thread.currentThread().getStackTrace()[2].getLineNumber();
            StackTraceElement stackTraceElement[] = Thread.currentThread().getStackTrace();
            for (int i = 0; i < stackTraceElement.length; i++) {
                if (stackTraceElement[i].getClassName().startsWith("com.qunar.train")
                        && !stackTraceElement[i].getClassName().startsWith("com.qunar.train.mock")) {
                    clazzName = stackTraceElement[i].getClassName();
                    methodName = stackTraceElement[i].getMethodName();
                    linenumber = stackTraceElement[i].getLineNumber();
                    break;
                }
            }

            System.err.println("<<<<<<<<<<<<<<< " + clazzName + '.' + methodName + " " + linenumber + "行 " + " exception : " + e.getMessage() + " >>>>>>>>>>>>>>>");
        } catch (Exception ex) {
            System.err.println("<<<<<<<<<<<<<<< AegisLog.error exception : " + ex.getMessage() + " >>>>>>>>>>>>>>>");
        }
    }

    public static void info(String str) {
        System.out.println("================================== " + str + " ==================================");
    }

    public static void log(Object obj) {
        try {
            String log = "[";
            String clazzName = Thread.currentThread().getStackTrace()[3].getClassName();
            String methodName = Thread.currentThread().getStackTrace()[3].getMethodName();
            StackTraceElement stackTraceElement[] = Thread.currentThread().getStackTrace();
            //System.out.println("++++++++++++++++++++++++++++++++++++++++++++++");
            //for(StackTraceElement ste : stackTraceElement){
            //    System.out.println(ste.getClassName());
            //}
            //System.out.println("++++++++++++++++++++++++++++++++++++++++++++++");
            for (int i = 0; i < stackTraceElement.length; i++) {
                if (stackTraceElement[i].getClassName().startsWith("com.qunar.train")
                        && !stackTraceElement[i].getClassName().startsWith("com.qunar.train.mock")) {
                    clazzName = stackTraceElement[i].getClassName();
                    methodName = stackTraceElement[i].getMethodName();
                    break;
                }
            }
            Date date = new Date();
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String tm = sdf.format(date);
            log = log + tm + "] [class=" + clazzName + " method=" + methodName + "] ";

            System.out.println(log + obj);
        } catch (Exception e) {
            AegisLog.error(e);
        }
    }

    public static void fatal(Object obj) {
        try {
            String log = "[";
            String clazzName = Thread.currentThread().getStackTrace()[3].getClassName();
            String methodName = Thread.currentThread().getStackTrace()[3].getMethodName();
            StackTraceElement stackTraceElement[] = Thread.currentThread().getStackTrace();
            //System.err.println("++++++++++++++++++++++++++++++++++++++++++++++");
            //for(StackTraceElement ste : stackTraceElement){
            //    System.err.println(ste.getClassName());
            //}
            //System.err.println("++++++++++++++++++++++++++++++++++++++++++++++");
            for (int i = 0; i < stackTraceElement.length; i++) {
                if (stackTraceElement[i].getClassName().startsWith("com.qunar.train")
                        && !stackTraceElement[i].getClassName().startsWith("com.qunar.train.mock")) {
                    clazzName = stackTraceElement[i].getClassName();
                    methodName = stackTraceElement[i].getMethodName();
                    break;
                }
            }
            Date date = new Date();
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String tm = sdf.format(date);
            log = log + tm + "] [class=" + clazzName + " method=" + methodName + "] ";

            System.err.println(log + obj);
        } catch (Exception e) {
            AegisLog.error(e);
        }
    }

    public static void error(String str) {
        System.err.println(str);
    }

    public static void println(String str) {
        System.out.println(str);
    }

}
